Week 4 of 16

How APIs Work

The request/response model — and your first look at calling Claude from Python

Day 16 60 minutes Watch + Read

Day 16 of 80

What You'll Accomplish Today

Why This Week Is Different

Weeks 1–3 were about Python itself — the syntax, the data structures, the thinking patterns. That was groundwork. This week, you connect Python to the outside world.

By Friday you'll have a working script that takes a shot description you type in plain English, sends it to Claude over the internet, and gets back a production-ready AI video prompt. This is a real tool — something you could use at work on Monday.

That shift — from learning Python to building things with Python — happens this week.

The Pattern You're Learning

Almost every AI tool you'll ever build follows the same pattern: (1) take some input, (2) format it into a request, (3) send it to an API, (4) extract the useful part of the response, (5) do something with it. Once you understand this loop, you can call Claude, OpenAI, Replicate, ElevenLabs, or any other AI service. They all work the same way.

Part 1: Watch — The requests Library (25 min)

Before you call Claude's API specifically, you need to understand how Python talks to the internet in general. Corey Schafer's tutorial on the requests library is the clearest explanation available.

Corey Schafer — Python Requests Tutorial

Watch the full video (~25 min)

What to Focus On

You don't need to absorb every detail — Corey covers a lot of ground. Focus on three things: (1) how requests.get(url) sends a request, (2) how you check if it worked with response.status_code, and (3) how you read JSON from the response with response.json(). The Claude SDK wraps all of this for you, but understanding the underlying mechanics will make everything else click faster.

Key Concepts from the Video

Part 2: Read — The Anthropic Quickstart

Now zoom in on the specific API you'll be using this week. The Anthropic Quickstart is short, well-written, and tells you exactly what to do.

Resource What to do
Anthropic Quickstart Read the full page. Note the install command, the import pattern, and the shape of client.messages.create().
Beginner's Tutorial for the Claude API Step-by-step walkthrough with explanations. Read this second — it fills in the "why" behind each step.
Don't Install Yet

Today is reading and understanding. You'll install the SDK and write real code tomorrow. If you jump ahead now, you'll have unanswered questions that make tomorrow harder. Let today's reading build the mental model first.

The Big Picture: What Is an API?

Key Concept: What Is an API?

An API is like a restaurant. You (your code) are the customer. You send an order (a request) to the kitchen (a server). The kitchen does the work and sends back food (a response). You never see the kitchen — you just send orders and get results back.

When your Python code calls Claude, it sends a request over the internet to Anthropic's servers. Claude processes it. Your code gets the response back. You didn't write Claude — you just ordered from it.

The "menu" — the list of things you can order and how to ask for them — is what we call the API. Anthropic's menu is documented at platform.claude.com/docs.

The Request/Response Cycle

Every API call follows this same four-step cycle:

  1. You build a request
    You specify what you want: which model, what system prompt, what the user said, how long the response should be.
  2. Your code sends it over the internet
    The SDK handles this — it turns your Python dictionary into an HTTP POST request, adds your API key to the headers, and sends it to Anthropic's servers.
  3. Claude processes it
    This happens on Anthropic's computers, not yours. You wait. (Usually 1–5 seconds for short responses.)
  4. You receive a response object
    The SDK unpacks the raw HTTP response into a Python object with attributes you can access. message.content[0].text is where the actual text lives.

Understanding the Response Object

The Response Object Structure

When Claude responds, you don't just get text back — you get a full object with metadata attached. Here's what that object looks like conceptually:

message
  ├── id               → unique ID for this API call
  ├── model            → which Claude model was used
  ├── stop_reason      → why it stopped ("end_turn", "max_tokens")
  ├── usage
  │     ├── input_tokens   → tokens in your request (you pay for these)
  │     └── output_tokens  → tokens in Claude's response (you pay for these)
  └── content          → list of content blocks
        └── [0]
              ├── type  → "text"
              └── text  → the actual response text ← THIS IS WHAT YOU WANT

This is why you write message.content[0].text — you're drilling into the object: get the content list, take the first item ([0]), get its text attribute. Every Claude API call uses this same path.

Why Is It a List?

message.content is a list because Claude can technically return multiple content blocks — for example, text plus tool use results. In practice, for the work you're doing this week, it's always a list with one item. But the SDK is designed for the general case, so you always have to do [0] to get the first (and usually only) block.

API Keys: What They Are

To call Claude's API, you need an API key — a long string that looks like sk-ant-api03-abc123.... This key is:

Tomorrow you'll get your key and set it up securely. For today, just understand what it is.

API Keys Are Not Free Forever

Anthropic offers free credits to new accounts — enough to do everything in this course with room to spare. After that, you pay per token (very cheap for learning-scale usage). Expect a few cents per session. You won't be surprised by a large bill if you use the SDK normally.

End of Day Checklist

Tomorrow

You install the Anthropic SDK, get your API key, and write your first real Claude API call. By the end of Day 17 you'll have a working Python script that talks to Claude — not a simulation, not a demo. The real thing.